package day10;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.Executors;

public class Demo10 {
    public static void main(String[] args) {
        /*
         * 遍历Map集合 的各种方法
         */
        Map<String, String> map = new HashMap<>();
        map.put("p1", "Tom");
        map.put("p2", "Jerry");
        map.put("p3", "Andy");
        map.put("p4", "Lee");

        System.out.println("传统迭代器, 迭代Entry");
        Iterator<Map.Entry<String,String>> iterator=
                map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry entry = iterator.next();
            System.out.println(entry);
        }
        

        System.out.println("传统迭代器, 迭代key");
        Iterator<String> keys = map.keySet().iterator();
        while (keys.hasNext()){
            String key = keys.next();
            System.out.println(key+":"+map.get(key));
        }

        System.out.println("foreach 迭代 entry");
        for (Map.Entry<String, String> entry: map.entrySet()){
            System.out.println(entry);
        }

        System.out.println("foreach 迭代 key");
        for (String key: map.keySet()){
            System.out.println(key + ":" + map.get(key));
        }

        System.out.println("Java 8 forEach 迭代Entry");
        map.forEach((key,value)-> System.out.println(key+":"+value));

        System.out.println("Java 8 forEach 迭代Key");
        map.keySet().forEach(key-> System.out.println(key+":"+map.get(key)));

        System.out.println("Java 8 forEach 迭代entrySet");
        map.entrySet().forEach(System.out::println);

        System.out.println("Java 8 entrySet().stream()");
        map.entrySet().stream().forEach(System.out::println);

        System.out.println("Java 8 keySet().stream()");
        map.keySet().stream().forEach(key-> System.out.println(key+":"+map.get(key)));
    }
}
